import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  BadRequestException,
} from '@nestjs/common';
import { ScenePathRelationalService } from './scene_path_relational.service';
import { CreateScenePathRelationalDto } from './dto/create-scene_path_relational.dto';
import { UpdateScenePathRelationalDto } from './dto/update-scene_path_relational.dto';
import { PathListService } from '../path-list/path-list.service';
import { SceneListService } from '../scene-list/scene-list.service';
import { Public } from 'src/common/decorator/public.decorator';
@Controller('scene-path-relational')
export class ScenePathRelationalController {
  constructor(
    private readonly scenePathRelationalService: ScenePathRelationalService,
    private readonly pathListService: PathListService,
    private readonly sceneListService: SceneListService,
  ) {}

  @Post('createRelation')
  async create(
    @Body() createScenePathRelationalDto: CreateScenePathRelationalDto,
  ) {
    try {
      // 查询轨道和场景是否存在
      const pathResult = await this.pathListService.findOne(
        createScenePathRelationalDto.path_id,
      );

      const sceneResult = await this.sceneListService.findOne(
        createScenePathRelationalDto.scene_id,
      );

      // 确保轨道和场景存在，然后创建关系
      if (pathResult && sceneResult) {
        if (
          !(await this.scenePathRelationalService.existsBySceneIdAndPathId(
            createScenePathRelationalDto.scene_id,
            createScenePathRelationalDto.path_id,
          ))
        ) {
          await this.scenePathRelationalService.create(
            createScenePathRelationalDto,
          );
          return 'Relation created successfully';
        } else {
          return 'Relation already exists';
        }
      } else {
        throw new BadRequestException('Invalid path or scene id');
      }
    } catch (error) {
      throw new BadRequestException('Failed to create relation');
    }
  }

  @Get()
  findAll() {
    return this.scenePathRelationalService.findAll();
  }

  @Get('getPathByPathIdsAndTypeBySceneId/:sceneId/:fileType')
  getPathByPathIdsAndTypeBySceneId(
    @Param('sceneId') scene_id: string,
    @Param('fileType') fileType: string,
  ) {
    return this.scenePathRelationalService.getPathByPathIdsAndTypeBySceneId(
      scene_id,
      fileType,
    );
  }

  @Public()
  @Get('getPathByPathIdsBySceneId/:sceneId')
  getPathByPathIdsBySceneId(@Param('sceneId') scene_id: string) {
    return this.scenePathRelationalService.getPathByPathIdsBySceneId(scene_id);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.scenePathRelationalService.findOne(id);
  }

  @Get('findBySceneId/:sceneId')
  findBySceneId(@Param('sceneId') scene_id: string) {
    return this.scenePathRelationalService.findBySceneId(scene_id);
  }

  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateScenePathRelationalDto: UpdateScenePathRelationalDto,
  ) {
    return this.scenePathRelationalService.update(
      id,
      updateScenePathRelationalDto,
    );
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.scenePathRelationalService.remove(id);
  }

  @Delete('delRelationByPathId/:pathId/:sceneId')
  delRelationByPathId(
    @Param('pathId') pathId: string,
    @Param('sceneId') sceneId: string,
  ) {
    return this.scenePathRelationalService.removeByPathId(pathId, sceneId);
  }
}
